

# coding:utf8

"""
pip install PyAutoit

"""
from project.SipMachine.pages.base_page import BasePage
from log import logger
import autoit
from time import sleep
from project.SipMachine.utils.locators import (LocalMainContain, LocalMainLeft, LocalLogin,
                                               LocalMainTOp, LocalMainVerson, LocalLogin)
from project.SipMachine.pages.basesip import ParamPublic
from project.SipMachine.pack.handle_data_file.handle import HandleSipFileAuto
from time import sleep


def set_popup_window_path(path: str):
    sleep(1)
    autoit.control_focus("打开", "[Class:Edit;instance:1]")
    # 输入文本
    sleep(0.5)
    autoit.control_set_text("打开", "[Class:Edit;instance:1]", path)
    #单击按钮
    sleep(0.5)
    autoit.control_click("打开", "[Class:Button; instance:1]")
    sleep(1)
    
    

class SipMainPage(BasePage):
    def __init__(self, driver, parampublic: ParamPublic):
        super(SipMainPage, self).__init__(driver)
        """
        parampublic: 公共参数
        """
        self.loc_main = LocalMainContain
        self.loc_login = LocalLogin
        self.parampublic = parampublic
        self.handlefile = HandleSipFileAuto(self.parampublic.file_path)
        # self.parampublic.siptype = self.handlefile.filetype
        self.parampublic.set_siptype(self.handlefile.filetype)

        
    def login(self):
        sleep(2)
        logger.debug("in to f -> login")
        # if not self.i_find_element(self.loc_login.username):
        #     logger.debug("不是在登陆界面~")
        #     return
        
        if self.i_find_element(self.loc_login.frame_name):
            logger.debug("在主界面,不用登陆了")
            return
        
        self.send_keys(self.loc_login.username, "admin")
        self.send_keys(self.loc_login.password, "admin")
        self.select_combox_index(self.loc_login.selectbox, 1)
        self.click_button(self.loc_login.check)
        self.click_button(self.loc_login.submit)
        
        
    def process_config(self):
        # 判断是否在这个页面
        sleep(2)
        self.switch_to_frame(LocalMainTOp)
        self.click_button(LocalMainTOp.tab_config)
        sleep(2)

        # switch contain
        self.switch_to_frame(LocalMainContain)

        # search file input element
        btn_select = self.i_find_element(LocalMainContain.Config.btn_select)
        logger.debug("是否找到输入文件路径的编辑框: {}".format(btn_select is not None))
        # run js
        self.driver.execute_script("arguments[0].style=arguments[1]", btn_select, "display: block;")
        sleep(2)
        # 点击，弹出文件选择对话框
        self.click_button(LocalMainContain.Config.btn)
        # 处理文件选择对话框
        set_popup_window_path(self.parampublic.file_path)
        
        # 导入配置
        self.click_button(self.loc_main.Config.btn_daoru)
        sleep(2)
        
    def quit(self):
        self.driver.quit()


    def get_version(self):
        return

    def process_update(self):


        pass

    def process_mt(self):
        sleep(2)
        
        logger.info("mt")
        # self.open_ip_from_file()
        
        # 切换`左侧边栏`
        self.switch_to_frame(LocalMainLeft)
        # 选择 `左边 对讲`
        self.click_button(LocalMainLeft.duijiang)
        # 切换 `顶`
        self.switch_to_frame(LocalMainTOp)
        # 选择 `媒体页`
        self.click_button(LocalMainTOp.DuiJiang.tab_meiti)
        
        # 切换 `内容`
        self.switch_to_frame(LocalMainContain)
        # 点击`媒体>>`
        self.click_button(LocalMainContain.Meiti.meiti_)
        
        # 选择 `媒体`
        self.select_by_value(LocalMainContain.Meiti.select_dis, "媒体")
        # 点击,`媒体`转向 右
        self.click_button(LocalMainContain.Meiti.btn_right)
        # 点击 `提交`
        self.click_button(LocalMainContain.Meiti.btn_accept)
        
        #　mt
        
        

    def open_ip_from_file(self):
        self.handlefile.handfile.rereadfile()
        ip = self.handlefile.handfile.ip
        logger.debug("read config-file, get ip : {}".format(ip))
        self.open_page_from_ip(ip)

    def process_quit_broadcast(self):
        # 判断是否在这个页面
        # 退出广播模式
        
        pass
    
    def login_out(self):
        pass
        


class LoginPage(BasePage):
    def __init__(self, driver):
        """ """
        super(LoginPage, self).__init__(driver)
        self.locator = LocalLogin

    def is_login_page(self):
        pass

    def login(self):
        logger.debug("f->login  1")
        if not self.is_exists_element(self.locator.username):
            logger.debug("f>login  2: 不是处在登陆界面")
            return
        logger.debug("f>login  3")
        # cookies_obj = Cookies(driver)
        # rev = cookies_obj.set_cookies()
        rev = 1
        if rev:
            self.send_keys(self.locator.username, "admin")
            self.send_keys(self.locator.password, "admin")
            self.select_combox_index(self.locator.selectbox, 1)
            self.click_button(self.locator.check)
            self.click_button(self.locator.submit)
        
        else:
            # cookies_obj.save()
            pass